package mop.model.task.state;

import java.util.Map;

import mop.exception.BusinessRule4Exception;
import mop.exception.RecursiveBusinessRule2Exception;
import mop.model.task.Task;
import mop.model.task.TaskInterface;

/**
 * State class for a failed task
 * 
 * @author 	MOP Team 7
 */
public class Failed extends State 
{
	
	/**
	 * Constructor
	 * @param t	Task for this state
	 * @post	Task is set to given task
	 * 	|super(t)
	 * 
	 */
	public Failed(Task t)
	{
		super(t);	
	}
	
	/**
	 * Constructor without exceptions !! Construction is Forced
	 * @param t Task for this state
	 * @param ignore boolean that tells us wether it's a forced construction
	 */
	public Failed(Task t,boolean ignore)
	{
		super(t,ignore);
	}
	
	/**
	 * Copy Constructor
	 * @param oldstate
	 * 
	 * @throws RecursiveBusinessRule2Exception
	 *  | @see rule2Parents()
	 *  
	 * @throws BusinessRule4Exception 
	 * 	| if(!checkReservations()) 
	 */
	public Failed(State oldstate)  throws RecursiveBusinessRule2Exception, BusinessRule4Exception
	{
		super(oldstate);
		
		/**
		 * The businessrule 4 exception
		 * when the task does not have the required number of valid resource reservations
		 */
		if(!rule4isMet())
		{
			throw new BusinessRule4Exception(this.getTask());
		}
			
		Map<TaskInterface,String> map = this.rule2Parents();
		if(map !=null)
		{
			throw new RecursiveBusinessRule2Exception(map);
		}
	}
	
	/***************************************************************************
	 * 						Status
	 ***************************************************************************/
	
	/**
	 * Use Case functionality
	 * Update details of task according to this state
     * @return returns false when details should not be updated
	 */
	public boolean updateDetails()
	{
		return false;
	}
		
	/**
	 * Check if dependencies are intact
	 */
	public State refreshStatus()
	{
		return this;
	}
	
	
	/**
	 * check on Business Rule nr 2
	 * @return for Failed this is always true so return null
	 */
	public Task rule2isMet()
	{
		return null;
	}
	
	/**
	 * return wether bussines rule nr 3 is met
	 * @return this is always true for failed
	 */
	public boolean rule3isMet()
	{
		return true;
	}
		
	
	/***************************************************************************
	 * 						Java object interface
	 ***************************************************************************/
	/**
	 * @see 	Object#toString()
	 */
	public String toString()
	{
		return "Failed";
	}
	
	/**
	 * return a string of the class name
	 * @return "Failed"
	 */
	public static String stateName()
	{
		return "Failed";
	}
}
